package com.bwie.rent.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.TypeReference;
import com.bwie.rent.entity.AddrsTo;
import com.bwie.rent.entity.EsHouseRent;
import com.bwie.rent.feign.AssistFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.rent.entity.RentEntity;
import com.bwie.rent.service.RentService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author lfw
 * @email sunlightcs@gmail.com
 * @date 2022-03-21 16:12:08
 */
@RestController
@RequestMapping("rent/rent")
public class RentController {
    @Autowired
    private RentService rentService;

    @Autowired
    AssistFeign assistFeign;

    /**
     * 详情信息
     */
    @RequestMapping("/findHouseById")
    public R findHouseById(@RequestParam(value = "hid")String hid) throws IOException {
        R r =rentService.findHouseById(hid);
        return r;
    }

    /**
     * 查找地址
     */
    @RequestMapping("/findAddr")
    public R findAddr(@RequestParam(value = "fid")String fid) throws IOException {
        R addrById = assistFeign.findAddrById(fid);
        if (addrById.get("code").equals(0)){
            List<AddrsTo> data = addrById.getData(new TypeReference<List<AddrsTo>>() {
            });
            return R.ok().put("addrs",data);
        }
        return R.error();
    }

    /**
     * es列表
     */
    @RequestMapping("/eslist")
    public R list(@RequestParam Map<String, Object> params,@RequestBody EsHouseRent esHouseRent) throws IOException {
        PageUtils page = rentService.findEsList(params,esHouseRent);
        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = rentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{houseId}")
    public R info(@PathVariable("houseId") Integer houseId){
		RentEntity rent = rentService.getById(houseId);

        return R.ok().put("rent", rent);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody RentEntity rent){
		rentService.save(rent);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RentEntity rent){
		rentService.updateById(rent);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] houseIds){
		rentService.removeByIds(Arrays.asList(houseIds));

        return R.ok();
    }

}
